package io.bear.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value = "接口统一响应对象", description = "接口统一响应对象")
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "响应信息")
    private String msg = "操作成功!";

    @ApiModelProperty(value = "响应编码")
    private Integer code = 200;

    @ApiModelProperty(value = "响应数据对象")
    private T data;

    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();


    public static <T> R<T> ok() {
        return ok(null);
    }

    public static <T> R<T> ok(T data) {
        return ok("操作成功!", data);
    }

    public static <T> R<T> ok(String msg, T data) {
        return ok(200, msg, data);
    }

    public static <T> R<T> ok(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> error() {
        return error("操作成功!");
    }

    public static <T> R<T> error(String msg) {
        return error(500, msg);
    }

    public static <T> R<T> error(Integer code, String msg) {
        return error(code, msg, null);
    }

    public static <T> R<T> error(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public R() {
    }

    public R(Integer code, String msg, T object) {
        this.data = object;
        this.code = code;
        this.msg = msg;
    }
}
